//
//  QSubordinateVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/3/2.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QSubordinateVC.h"
#import "QMySuperiorCell.h"
#import "QSubordinateCell.h"
#import "QOrderListModel.h"
#import "QCuOrderModel.h"
@interface QSubordinateVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _index;
    NSMutableArray *_dataArr;
    UILabel *_titleLab;
}
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)QOrderListModel *orderListModel;
@end

@implementation QSubordinateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"下级明细";
    _dataArr = [NSMutableArray array];
    [self createUI];
    [self requestData];
    _tbView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tbView.mj_footer resetNoMoreData];
        [self requestData];
    }];
    
    _tbView.mj_footer = [QViewUtility showFootRefresh:^{
        [self requestMoreData];
    }];

}

- (void)createUI{
    self.view.clipsToBounds = YES;
    [self.view addSubview:self.tbView];
    
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.tbView registerNib:[UINib nibWithNibName:@"QMySuperiorCell" bundle:nil] forCellReuseIdentifier:@"QMySuperiorCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QSubordinateCell" bundle:nil] forCellReuseIdentifier:@"QSubordinateCell"];
    UIView *header =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    _titleLab =[QViewUtility createLabelWithFrame:CGRectMake(20, 0, kScreenWidth-20, 50) font:Ksize12 textAlignment:0 textColor:[QColorUtility defaultDeepGrayColor] text:@""];
    _titleLab.numberOfLines =0;
    [header addSubview:_titleLab];
    
    self.tbView.tableHeaderView =header;

}


- (void)requestData{
    _index = 1;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMyCommissionOrders"];
    NSDictionary *para =[QParamService getMyCommissionOrdersWithCommissionId:self.cuId Start:start PageSize:KpageSize];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility dismissNoDataViewInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [_dataArr removeAllObjects];
            
            _orderListModel =[QOrderListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:_orderListModel.orders];
            
            if(_dataArr.count == 0){
                [QViewUtility showNoDataViewInView:_tbView string:@""];
            }else{
                if(_orderListModel.orders.count < 10){
                    [_tbView.mj_footer noticeNoMoreData];
                }
            }
            [_tbView reloadData];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

- (void)requestMoreData{
    _index++;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMyCommissionOrders"];
    NSDictionary *para =[QParamService getMyCommissionOrdersWithCommissionId:self.cuId Start:start PageSize:KpageSize];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_footer endRefreshing];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
            _orderListModel = [QOrderListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:_orderListModel.orders];
            if(_orderListModel.orders.count < 10){
                [_tbView.mj_footer noticeNoMoreData];
                //[QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"没有更多了" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    }else{
        
        return 40;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (_dataArr.count) {
            _titleLab.text = @"APP端仅显示本月客户下单明细，查看更多订单请联系客服!";
            return 1;
        }
        return 0;
    }else{
        return _dataArr.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        QMySuperiorCell * cell= [tableView dequeueReusableCellWithIdentifier:@"QMySuperiorCell" forIndexPath:indexPath];
        if (_dataArr.count) {
            QCuOrderModel * model = _dataArr[indexPath.section];
            cell.model=model;
        }
        return cell;
    }else{
        QSubordinateCell *cell =[tableView dequeueReusableCellWithIdentifier:@"QSubordinateCell"];

        QCuOrderModel *model = _dataArr[indexPath.row];
        cell.model = _dataArr[indexPath.row];
        if ([_orderListModel.now isEqualToString:model.addTime]) {
            cell.titleLab.text = @"今天";
        }else{
            cell.titleLab.text = model.addTime;
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        QCuOrderModel * model = _dataArr[indexPath.section];
        [QViewUtility showPhonePlayBoxViewWithName:model.name Number:model.tell myBlock:^(NSInteger index) {
            if (index == 1) {
                [QViewUtility callPhoneWithNumber:model.tell];
            }
        }];

        
    }
}

- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
